package com.trinity.task;

import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryV3Result;
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.trinity.order.refund.domain.XcRefundOrder;
import com.trinity.order.refund.service.IXcRefundOrderService;
import com.trinity.orders.constant.OrderConstants;
import com.trinity.orders.domain.XcOrders;
import com.trinity.orders.domain.XcPayRecord;
import com.trinity.orders.service.IXcOrdersService;
import com.trinity.orders.service.IXcPayRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Slf4j
@Component
public class WxOrderTask {
    @Resource
    private IXcOrdersService ordersService;
    @Resource
    private IXcRefundOrderService refundOrderService;
    @Resource
    private IXcPayRecordService payRecordService;
    @Resource
    private WxPayService wxService;

    public void queryPayOrderTask() {
        //超时20分钟的订单
        List<XcPayRecord> payRecordList = payRecordService.selectPayRecordNoPayDuration(5);
        for (XcPayRecord payRecord : payRecordList) {
            // 核实 订单状态
            String payNo = payRecord.getPayNo();
            String orderNo = payRecord.getOrderId();
            try {
                log.info("\n核实支付订单状态：========> {}", payNo);
                WxPayOrderQueryV3Result result= wxService.queryOrderV3("", payNo);
                //核实订单已支付
                if (OrderConstants.TradeState.SUCCESS.equals(result.getTradeState())) {
                    log.info("\n核实支付订单已支付：========> {}", payNo);
                    //订单已支付 更新本地订单状态
                    payRecord.setOutPayNo(result.getTransactionId());
                    payRecord.setStatus(result.getTradeState());
                    String timeEnd = result.getSuccessTime();
                    payRecord.setPaySuccessTime(timeEnd);
                    int i = payRecordService.updateXcPayRecord(payRecord);
                    XcOrders xcOrders = ordersService.selectXcOrdersById(orderNo);
                    xcOrders.setStatus(result.getTradeState());
                    int i1 = ordersService.updateXcOrders(xcOrders);
                }
                //核实订单未支付
                if (OrderConstants.TradeState.NOTPAY.equals(result.getTradeState())) {
                    log.info("\n核实支付订单未支付：========> {}", payNo);
                    //关闭订单
                    wxService.closeOrderV3(payNo);
                    //更新本地订单状态
                    payRecord.setStatus(OrderConstants.TradeState.CLOSED);
                    int i = payRecordService.updateXcPayRecord(payRecord);
                }
            } catch (WxPayException e) {
                if ("ORDERNOTEXIST".equals(e.getErrCode())) {
                    //更新本地订单状态
                    payRecord.setStatus(OrderConstants.TradeState.PAYERROR);
                    int i = payRecordService.updateXcPayRecord(payRecord);
                }
                log.error("微信查单失败！订单号：{},原因:{}，代码：{}", orderNo, e.getMessage(),e.getErrCode());
            }

        }
    }

    public void queryRefundOrderTask() throws ParseException {
        List<XcRefundOrder> refundOrders = refundOrderService.selectRefundOrderDuration(5);
        for (XcRefundOrder refund : refundOrders) {
            // 核实 订单状态
            String refundNo = refund.getOutRefundNo();
            String orderNo = refund.getOrderId();
            try {
                log.info("核实退款订单状态：========> {}", orderNo);
                WxPayRefundQueryV3Result result = wxService.refundQueryV3(refundNo);
                //核实订单已支付
                if (OrderConstants.RefundState.SUCCESS.equals(result.getStatus())) {
                    log.info("核实订单已退款：========> {}", orderNo);
                    //订单已支付 更新本地订单状态
                    refund.setRefundId(result.getRefundId());
                    refund.setStatus(result.getStatus());
                    String timeEnd = result.getSuccessTime();
                    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
                    Date date = inputFormat.parse(timeEnd);
                    refund.setSuccessTime(date);
                    int i = refundOrderService.updateXcRefundOrder(refund);
                    XcOrders xcOrders = ordersService.selectXcOrdersById(orderNo);
                    xcOrders.setStatus(OrderConstants.TradeState.REFUND);
                    int i1 = ordersService.updateXcOrders(xcOrders);
                }
                //核实订单未支付
//                if (OrderConstants.RefundState.PROCESSING.equals(result.getStatus())) {
//                    log.info("核实订单未退款：========> {}", orderNo);
//                    //关闭订单
//                    wxService.closeOrderV3(orderNo);
//                    //更新本地订单状态
//                    refund.setStatus(OrderConstants.RefundState.CLOSED);
//                    int i = refundOrderService.updateXcRefundOrder(refund);
//                }
            } catch (WxPayException e) {
                //更新本地订单状态
                refund.setStatus(OrderConstants.RefundState.ABNORMAL);
                int i = refundOrderService.updateXcRefundOrder(refund);
                log.error("微信查单失败！订单号：{},原因:{}", orderNo, e.getMessage());
//                e.printStackTrace();
//            return AjaxResult.error("支付失败，请稍后重试！");
            }

        }
    }
}
